"use strict";

/**
 * @ngdoc directive
 * @name jgappApp.directive:telDirective
 * @description
 * # telDirective
 */
angular
  .module("jgappApp")
  .directive("telDirective", function($parse) {
    return {
      require: "ngModel",
      restrict: "AE",
      link: function postLink(scope, element, attrs, ngModelController) {
        var reg = /^(([1-9]+\.?)|0)\d*$/i;
        // element.bind("input", function() {
        //     if (!reg.test(this.value)) {
        //         this.value = parseFloat(this.value) || ""
        //     }
        // })
        element.bind("blur", function() {
          // if (!reg.test(this.value)) {
          var data = parseFloat(this.value, 10);
          if (data === 0) {
            data = 0;
          } else if (!data) {
            data = "";
          }
          var modelGetter = $parse(attrs["ngModel"]);
          console.log(modelGetter(scope));
          // This returns a function that lets us set the value of the ng-model binding expression:
          var modelSetter = modelGetter.assign;
          // This is how you can use it to set the value 'bar' on the given scope.
          modelSetter(scope, data);
          scope.$apply();
          // }
        });
      }
    };
  })
  .directive("errSrc", function() {
    return {
      link: function(scope, element, attrs) {
        element.bind("error", function() {
          element.remove();
        });
      }
    };
  });
